#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cstring>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;

int dx[4] = {0,0,-1,1};
int dy[4] = {1,-1,0,0};
struct node {
	int x,y,step;
};
int sx,sy,ex,ey;
int n,m;
vector<vector<char>> v(107,vector<char>(107,0)),vis(107,vector<char>(107,0));

//dfs部分开始 （此题不能用dfs 只是思想）
/*
int ans = 0x3f;
void dfs(int x,int y,int step){
	if(x == ex&&y == ey){
		ans = min(ans,step);
		return;
	}
	for(int i = 0;i<4;i++){
		int tx = x+dx[i];
		int ty = y+dy[i];
		int ts = step+1;
		if((0<=tx&&tx<n&&0<=ty&&ty<m&&v[tx][ty] == '.'&&vis[tx][ty] == 0)){
			vis[tx][ty] = 1;
			dfs(tx,ty,ts);
			vis[tx][ty] = 0;
		}
	}
}
*/
//dfs部分结束

void solve() {
	cin>>n>>m;
	for(int i = 0;i<n;i++){
		for(int j = 0;j<m;j++){
			cin>>v[i][j];
			if(v[i][j] == 'S'){
				sx = i,sy = j;
			}
			if(v[i][j] == 'T'){
				ex = i,ey = j;
				v[i][j] = '.';
			}
		}
	}
	
	//dfs部分开始
	/*
	dfs(sx,sy,0);
	vis[sx][sy] = 1;
	if(ans!=0x3f){
		cout<<ans<<endl;
	}else cout<<"-1"<<endl;
	*/
	//dfs部分结束
	
	//bfs部分开始
	bool flag = 0;
	queue<node> q;
	q.push({sx,sy,0});
	while(!q.empty()){
		node t = q.front();
		q.pop();
		if(t.x == ex&&t.y == ey){
			flag = 1;
			cout<<t.step<<endl;
			break;
		}
		if(v[t.x][t.y] == '.'||v[t.x][t.y] == 'S'){
			v[t.x][t.y] = '*';
			for(int i = 0;i<4;i++){
				int tx = t.x+dx[i];
				int ty = t.y+dy[i];
				int ts = t.step+1;
				if((0<=tx&&tx<n&&0<=ty&&ty<m&&v[tx][ty] == '.')){
					q.push({tx,ty,ts});
				}
			}
		}
	}
	if(!flag){
		cout<<"-1"<<endl;
	}
	//bfs部分结束
	
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int count = 1;
  //cin >> count;
  while (count--) {
    solve();
  }
  return 0;
}